// pages/combine/combine.js
const app=getApp();
Page({

  data: {
  
  },

  onLoad: function (options) {
    wx.getImageInfo({
      src:app.globalData.bgPic,
      success: res => {
          this.bgPic=res.path
        this.draw();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  
  draw() {
    let scale = app.globalData.scale;
    let rotate = app.globalData.rotate;
    let hat_center_x = app.globalData.hat_center_x;
    let hat_center_y = app.globalData.hat_center_y;
    let currentHatId = app.globalData.currentHatId;
    const pc = wx.createCanvasContext('myCanvas');
    const windowWidth = wx.getSystemInfoSync().windowWidth;
    const hat_size = 100 * scale;


    pc.clearRect(0, 0, windowWidth, 300);
    pc.drawImage(this.bgPic, windowWidth / 2 - 150, 0, 300, 300);
    pc.translate(hat_center_x,hat_center_y);
    pc.rotate(rotate * Math.PI / 180);
    pc.drawImage("../../images/" + currentHatId + ".png", -hat_size / 2-1, -hat_size / 2-120, hat_size, hat_size);
    pc.draw();
  },
  savePic() {
    const windowWidth = wx.getSystemInfoSync().windowWidth;
    wx.canvasToTempFilePath({
      x: windowWidth / 2 - 150,
      y: 0,
      height: 300,
      width: 300,
      canvasId: 'myCanvas',
      success: (res) => {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.showModal({
              title: '提示',
              content: '保存成功，前往相册查找',
              showCancel: false,
            }),
            wx.navigateTo({
                    url: '../index/index',
                    success: function(res) {},
                    fail: function(res) {},
                    complete: function(res) {},
                  })
          },
          fail() {
            wx.showModal({
              title: '提示',
              content: '需要您授权保存相册',
              showCancel: false,
              success() {
                wx.openSetting({
                  success(settingdata) {
                    if (settingdata.authSetting['scope.writePhotosAlbum']) {
                      wx.showModal({
                        title: '提示',
                        content: '获取权限成功,再次保存图片即可成功',
                        showCancel: false,
                      })
                    } else {
                      wx.showModal({
                        title: '提示',
                        content: '获取权限失败，无法保存到相册',
                        showCancel: false,
                      })
                    }
    
                  },
                  complete(comp) {
                    console.log("complete", finishData)
                  }
                })
              }
            })
          }
    
        })
        // wx.saveImageToPhotosAlbum({
        //   filePath: res.tempFilePath,
        //   success: (res) => {
        //     wx.navigateTo({
        //       url: '../index/index',
        //       success: function(res) {},
        //       fail: function(res) {},
        //       complete: function(res) {},
        //     })
        //     console.log("success:" + res);
        //   }, fail(e) {
        //     console.log("err:" + e);
        //   }
        // })
      }
    });
}
})